package market

import (
	"account/models"
	"account/server"
	"fmt"
	"net/http"
	"strconv"
	"strings"

	"github.com/gin-gonic/gin"
)

// @Summary 获取停用平台信息
// @Description get marekt info
// @Accept  json
// @Produce json
// @Success 200 {string} string "success"
// @Router /api/v1/market/black [get]
func MarketBlackAllInfos(ctx *gin.Context) {
	var response *server.Response

	result, err := server.GetMarketBlackAllInfos()
	if err != nil {
		message := fmt.Sprintf("没有获取导数据 :%s", err.Error())
		response = server.NewResponse(http.StatusNoContent, message, result)
		ctx.JSON(http.StatusNoContent, response)
		return

	}
	message := "success"
	response = server.NewResponse(http.StatusOK, message, result)
	ctx.JSON(http.StatusOK, response)
}

// @Summary 获取所有平台信息
// @Description get marekt info
// @Accept  json
// @Produce json
// @Success 200 {string} string "success"
// @Router /api/v1/market [get]
func MarketAllInfos(ctx *gin.Context) {

	var response *server.Response

	result, err := server.GetMarketAllInfos()
	if err != nil {
		message := fmt.Sprintf("没有获取导数据 :%s", err.Error())
		response = server.NewResponse(http.StatusNoContent, message, result)
		ctx.JSON(http.StatusNoContent, response)
		return

	}
	message := "success"
	response = server.NewResponse(http.StatusOK, message, result)
	ctx.JSON(http.StatusOK, response)
}

// @Summary 新增市场信息
// @Description get marekt info
// @Accept  json
// @Produce json
// @Param market body models.Market true "Market"
// @Success 200 {string} string "success"
// @Router /api/v1/market/add [post]
func MarketAdd(ctx *gin.Context) {
	var response *server.Response
	var market models.Market
	var err error

	market.Active, err = strconv.ParseBool(ctx.DefaultQuery("active", "1"))
	if err != nil {
		message := fmt.Sprintf("请求参数异常: %s", err.Error())
		response = server.NewResponse(http.StatusBadRequest, message, []interface{}{})
		ctx.JSON(http.StatusBadRequest, response)
		return
	}

	market.Deleted, err = strconv.ParseBool(ctx.DefaultQuery("deleted", "0"))
	if err != nil {
		message := fmt.Sprintf("请求参数异常: %s", err.Error())
		response = server.NewResponse(http.StatusBadRequest, message, []interface{}{})
		ctx.JSON(http.StatusBadRequest, response)
		return
	}

	if err := ctx.Bind(&market); err != nil {
		message := fmt.Sprintf("请求参数异常: %s", err.Error())
		response = server.NewResponse(http.StatusBadRequest, message, []interface{}{})
		ctx.JSON(http.StatusBadRequest, response)
		return
	}

	if err := server.AddMarketInfo(market); err != nil {
		message := fmt.Sprintf("添加信息失败 : %s", err)
		response = server.NewResponse(http.StatusExpectationFailed, message, []interface{}{})
		ctx.JSON(http.StatusExpectationFailed, response)
		return
	}

	message := "success"
	response = server.NewResponse(http.StatusOK, message, market)
	ctx.JSON(http.StatusOK, response)
}

// @Summary 更新市场信息
// @Description get marekt info
// @Accept  json
// @Produce json
// @Param market body models.Market true "Market"
// @Param id path int true "Plat Id"
// @Success 200 {string} string "success"
// @Router /api/v1/market/update/:id [post]
func MakretUpdateInfo(ctx *gin.Context) {
	/**
	需要两个参数：
	  1. 路径参数告知ID
	  2. 表单参数更新数据
	  --------------------
	  同样改接口可以走是否激活
	*/

	var response *server.Response
	var market models.Market

	platId := ctx.Param("id")
	if strings.Compare(platId, "") == 0 {
		message := "缺少必传参数"
		response = server.NewResponse(http.StatusBadRequest, message, []interface{}{})
		ctx.JSON(http.StatusBadRequest, response)
		return
	}

	id, err := strconv.Atoi(platId)
	if err != nil {
		message := fmt.Sprintf("数据格式转换失败 : %s", err.Error())
		response = server.NewResponse(http.StatusGone, message, []interface{}{})
		ctx.JSON(http.StatusGone, response)
		return
	}

	if err := ctx.Bind(&market); err != nil {
		message := fmt.Sprintf("请求参数异常 : %s", err.Error())
		response = server.NewResponse(http.StatusBadRequest, message, []interface{}{})
		ctx.JSON(http.StatusBadRequest, response)
		return

	}

	if err := server.UpdateMarketInfo(id, market); err != nil {
		message := fmt.Sprintf("更新信息失败 : %s", err.Error())
		response = server.NewResponse(http.StatusExpectationFailed, message, []interface{}{})
		ctx.JSON(http.StatusExpectationFailed, response)
		return

	}

	message := "success"
	response = server.NewResponse(http.StatusOK, message, market)
	ctx.JSON(http.StatusOK, response)
}

// @Summary 删除市场信息
// @Description get marekt info
// @Accept  json
// @Produce json
// @Param id path int true "Plat Id"
// @Success 200 {string} string "success"
// @Router /api/v1/market/active/:id [post]
func MarketActive(ctx *gin.Context) {
	var response *server.Response

	marketId := ctx.Param("id")
	if strings.Compare(marketId, "") == 0 {
		message := "缺少必传参数"
		response = server.NewResponse(http.StatusBadRequest, message, []interface{}{})
		ctx.JSON(http.StatusBadRequest, response)
		return
	}

	id, _ := strconv.Atoi(marketId)

	if err := server.ActiveMarketInfo(id); err != nil {
		message := fmt.Sprintf("更新信息失败 %s", err.Error())
		response = server.NewResponse(http.StatusExpectationFailed, message, []interface{}{})
		ctx.JSON(http.StatusExpectationFailed, response)
		return

	}

	message := "success"
	response = server.NewResponse(http.StatusOK, message, []interface{}{})
	ctx.JSON(http.StatusOK, response)
}
